package com.daka.controller;

import com.daka.annotation.Login;
import com.daka.entity.Organization;
import com.daka.enumerate.CommonStatus;
import com.daka.enumerate.StatusCode;
import com.daka.service.OrganizationService;
import com.daka.vo.request.OrganizationPageRequestVo;
import com.daka.vo.response.DataResponseVo;
import com.daka.vo.response.ListDataResponseVo;
import com.daka.vo.response.PageListResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;


/**
 * @author beliveli on 2016/3/31 0031.
 */
@Controller
@RequestMapping("/org")
@Slf4j
public class OrganizationController {

    @Resource(name = "organizationService")
    private OrganizationService organizationService;

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/list", consumes = "application/json")
    @Login
    public ListDataResponseVo<Organization> list(@Valid @RequestBody Organization organization, HttpSession session) {
        int orgId;
        if (organization.getOrgId() != 0) {
            orgId = organization.getOrgId();
        } else {
            orgId = Integer.parseInt(session.getAttribute("orgId").toString());
        }
        List<Organization> organizationList = organizationService.selectAllChild(orgId);
        return new ListDataResponseVo<>(StatusCode.SUCCESS.getStatusCode(), StatusCode.SUCCESS.getStatusMessage(), organizationList);
    }

    /**
     * 在新代码里不再使用
     * @param organization
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/test", consumes = "application/json")
    @Login
    public ListDataResponseVo<Organization> test(@Valid @RequestBody Organization organization, HttpSession session) {
        int orgId;
        if (organization.getOrgId() != 0) {
            orgId = organization.getOrgId();
        } else {
            orgId = Integer.parseInt(session.getAttribute("orgId").toString());
        }
        List<Organization> organizationList = organizationService.selectAllChild(orgId);
        return new ListDataResponseVo<>(StatusCode.SUCCESS.getStatusCode(), StatusCode.SUCCESS.getStatusMessage(), organizationList);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/listByPage", consumes = "application/json")
    @Login
    public DataResponseVo<PageListResponse<Organization>> listByPage(@Valid @RequestBody OrganizationPageRequestVo organization, HttpSession session) {
        int orgId;
        if (organization.getOrgId() != 0) {
            orgId = organization.getOrgId();
        } else {
            orgId = Integer.parseInt(session.getAttribute("orgId").toString());
        }
        List<Organization> topicList = organizationService.selectAllChild(orgId, organization.getOrgName(), organization.getPageIndex());
        int count = organizationService.selectCount(orgId, organization.getOrgName());
        PageListResponse<Organization> page = new PageListResponse<>(topicList, count);

        return new DataResponseVo<>(StatusCode.SUCCESS.getStatusCode(), StatusCode.SUCCESS.getStatusMessage(), page);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/save", consumes = "application/json")
    @Login
    public DataResponseVo<Organization> save(@Valid @RequestBody Organization organization, HttpSession session) {

        int orgParent = Integer.parseInt(session.getAttribute("orgId").toString());
        int userId = Integer.parseInt(session.getAttribute("userId").toString());
        Organization result = organizationService.save(organization, orgParent, userId);

        if (result == null) {
            return new DataResponseVo<>(StatusCode.JAVA_THROW_EXCEPTION.getStatusCode(), StatusCode.JAVA_THROW_EXCEPTION.getStatusMessage());
        }

        return new DataResponseVo<>(StatusCode.SUCCESS.getStatusCode(), StatusCode.SUCCESS.getStatusMessage(), result);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/info", consumes = "application/json")
    @Login
    public DataResponseVo<Organization> info(@Valid @RequestBody Organization organization) {

        Organization result = organizationService.selectById(organization.getOrgId());

        if (result == null) {
            return new DataResponseVo<>(StatusCode.JAVA_THROW_EXCEPTION.getStatusCode(), StatusCode.JAVA_THROW_EXCEPTION.getStatusMessage());
        }

        return new DataResponseVo<>(StatusCode.SUCCESS.getStatusCode(), StatusCode.SUCCESS.getStatusMessage(), result);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/updateStatus", consumes = "application/json")
    @Login
    public DataResponseVo<Organization> updateStatus(@Valid @RequestBody Organization organization) {

        Organization result = organizationService.selectById(organization.getOrgId());

        boolean statusFlag = false;
        List<Organization> organizationList = organizationService.selectAllChildNoSelf(organization.getOrgId());
        for (Organization org : organizationList) {
            if (org.getStatus() == CommonStatus.NORMAL.getStatusCode()) {
                statusFlag = true;
            }
        }

        if (statusFlag && result.getStatus() == CommonStatus.NORMAL.getStatusCode()) {
            return new DataResponseVo<>(StatusCode.DEDELE_ERROE.getStatusCode(), "该组织下存在解冻状态的组织，不能置为冻结");
        }

        result = organizationService.updateStatus(organization.getOrgId());


        return new DataResponseVo<>(StatusCode.SUCCESS.getStatusCode(), StatusCode.SUCCESS.getStatusMessage(), result);
    }
}
